package com.yifi.common.utils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;

public class ImageUtils {
	
	public static HashMap<String,String> getImgProp(File img)
	{
		HashMap<String,String> map=new HashMap<String,String>();
		Metadata metadata;
		try {
			metadata = JpegMetadataReader.readMetadata(img);
			//System.out.println("Directory Count: "+metadata.getDirectoryCount());
			//System.out.println();
			//输出所有附加属性数据
			for (Directory directory : metadata.getDirectories()) {
				//System.out.println("******\t" + directory.getName() + "\t******");
				for (Tag tag : directory.getTags()) {
					System.out.println(tag.getTagName() + ":" + tag.getDescription());
					map.put(tag.getTagName(), tag.getDescription());
				}
			}
			
			return map;
		} catch (JpegProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	public static HashMap<String,String> getImgProp(String imgPath)
	{
		File file=new File(imgPath);
		if(!file.exists())
		{
			return null;
		}
		HashMap<String,String> map=new HashMap<String,String>();
		Metadata metadata;
		try {
			metadata = JpegMetadataReader.readMetadata(file);
			//System.out.println("Directory Count: "+metadata.getDirectoryCount());
			//System.out.println();
			//输出所有附加属性数据
			for (Directory directory : metadata.getDirectories()) {
				//System.out.println("******\t" + directory.getName() + "\t******");
				for (Tag tag : directory.getTags()) {
					System.out.println(tag.getTagName() + ":" + tag.getDescription());
					map.put(tag.getTagName(), tag.getDescription());
				}
			}
			
			return map;
		} catch (JpegProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public static String getParam(String param,File file)
	{
		HashMap<String, String> map = getImgProp(file);
		return map.get(param);
	}
	
	public static Double getDouble(String ll)
	{
		int ll1=Integer.parseInt(ll.substring(0,ll.indexOf("°")).trim());
		double ll2=Double.parseDouble(ll.substring(ll.indexOf("°")+1,ll.indexOf("'")).trim())/60;
		double ll3=Double.parseDouble(ll.substring(ll.indexOf("'")+1,ll.indexOf("\"")).trim())/60/60;
		return ll1+ll2+ll3;
	}
	
	public static void main(String[] args) {
		getImgProp("C:\\Users\\Administrator\\Pictures\\111.jpg");
		//System.out.println(getDouble("116° 34' 33.39\""));
	}
}
